package com.quanyan.user.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.user.model.vo.response.RespCheckVersion;
import com.quanyan.user.model.vo.response.RespCommonParameter;
import com.quanyan.user.model.vo.response.RespSituation;
import com.quanyan.user.model.vo.response.YoumiResult;
import com.quanyan.user.request.ReqUserInvite;
import com.quanyan.user.response.RespUserInvite;
import com.quanyan.user.service.UserInformationService;
import com.quanyan.user.service.UserInviteService;
import com.quanyan.user.service.UserToCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by zhang on 16/6/28.
 */
@RestController
@RequestMapping("/api/user/base")
public class UserBaseController {

    @Autowired
    private UserInformationService userInformationService;
    @Autowired
    private UserInviteService userInviteService;
    @Autowired
    private UserToCService userToCService;

    /**
     * 获取城市列表
     * @return
     */
    @RequestMapping(value = "situation",method = RequestMethod.POST)
    public APIResponse<RespSituation> situation(){
        RespSituation respSituation = this.userInformationService.situation();
        return APIResponse.returnSuccess(respSituation);
    }

    /**
     * 获取用户公共参数
     * @param map
     * @return
     */
    @RequestMapping(value = "parameter",method = RequestMethod.POST)
    public APIResponse<List<RespCommonParameter>> parameter(@RequestBody Map map){
        List<RespCommonParameter> respCommonParameterList;
        if (map.containsKey("parameterKey")) {
            respCommonParameterList = this.userInformationService.parameter(map);
        }else{
            return APIResponse.returnFail("请传入必要参数");
        }
        return APIResponse.returnSuccess(respCommonParameterList);
    }

    /**
     * 用户邀请
     * @param reqUserInvite
     * @return
     */
    @RequestMapping(value = "invite",method = RequestMethod.POST)
    public APIResponse invite(@RequestBody ReqUserInvite reqUserInvite){
        RespUserInvite respUserInvite = userInviteService.isNewUser(reqUserInvite);
        return APIResponse.returnSuccess(respUserInvite);
    }

    /**
     * 检查版本更新
     * @param map
     * @return
     */
    @RequestMapping(value = "checkVersion",method = RequestMethod.POST)
    public APIResponse<RespCheckVersion> checkVersion(@RequestBody Map map){
        int version = (int) map.get("version");
        int osType = (int) map.get("osType");
        Integer appType = (Integer) map.get("appType");
        RespCheckVersion respCheckVersion = this.userInformationService.checkVersion(version,osType,appType);
        return APIResponse.returnSuccess(respCheckVersion);
    }

    /**
     * 邀请好友送鸟蛋
     * @param reqUserInvite
     * @return
     */
    @RequestMapping(value = "inviteUser",method = RequestMethod.POST)
    public APIResponse inviteUser(@RequestBody ReqUserInvite reqUserInvite){
        //RespUserInvite respUserInvite = userInviteService.inviteUserSendEggs(reqUserInvite);
        //respUserInvite
        return APIResponse.returnSuccess();
    }

    /**
     * 解决用户图像问题
     * @param map
     * @return
     */
    @RequestMapping(value = "updateUserIcon",method = RequestMethod.POST)
    public APIResponse updateUserIcon(@RequestBody Map map){
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        String keyWord = (String) map.get("keyWord");

        try {
            userToCService.disposeUserIcon(pageNum,pageSize,keyWord);
        } catch (Exception e) {
            return APIResponse.returnFail("处理失败！");
        }
        return APIResponse.returnSuccess();
    }

    /**
     * 有米渠道统计
     * @param mac
     * @param ifa
     * @param callback_url
     * @return
     */
    @RequestMapping(value = "youmiPromotion",method = RequestMethod.GET)
    public APIResponse youmiPromotion(String mac,String ifa,String callback_url){
        int n ;
        try {
            n = userToCService.addYoumiPromotion(mac,ifa,callback_url);
        } catch (Exception e) {
            return APIResponse.returnFail(ApiResponseEnum.FAIL);
        }
        if (n > 0) {
            return APIResponse.returnSuccess();
        } else {
            return APIResponse.returnFail(ApiResponseEnum.FAIL);
        }
    }

    @RequestMapping(value = "test")
    public YoumiResult test(){
        YoumiResult result = new YoumiResult();
        result.setC(0);
        return result;
    }

}
